﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using SourceCode.Workflow.Client;
using WF.Common;
using System.Data;
using WF.Web.Pages;
using System.Configuration;
using WF.Common;
using Com.DAO;

namespace WF.Web
{
    public partial class MainForm : BasePage
    {
        protected string DeptPathNolimited = "";

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack || this.IsRebind)
            {
                this.LoadWorkList();
            }
            ShowButton1();
        }

        private void LoadWorkList()
        {
            Connection oK2Connection = WF.Common.WorkFlowHelper.GetK2Connection();
            WF.BusinessObject.SystemSetting sysSet = new BusinessObject.SystemSetting();
            string k2folder = sysSet.GetValueByParamName("K2WorkflowFolder");
            try
            {
                DataSet dsMe = null, dsShare = null, dsShareUser;

                SessionManager sm = new SessionManager();

                //正常的待办事项
                dsMe = WorkFlowHelper.GetCurrentUserWorkListItem(oK2Connection, k2folder, sm.CurrentUser.LoginName);


                //委托的待办事项
                oK2Connection.RevertUser();
                dsShareUser = WorkFlowHelper.GetWorkListSharedADUser(null, sm.CurrentUser.LoginName);
                dsShare = WorkFlowHelper.GetCurrentUserWorkListItem(oK2Connection, k2folder, sm.CurrentUser.LoginName, dsShareUser);


                //所有分配的待办事项
                DataSet dsAll = new DataSet();
                dsAll.Merge(dsMe);
                dsAll.Merge(dsShare);

                DataView dv = dsAll.Tables[0].DefaultView;
                dv.Sort = "ProcessName,StartDate Desc,RegionName,Folio";

                this.dataListView.DataSource = dv;
                this.dataListView.DataBind();

                //this.lblCount.Text = "(" + dv.Count.ToString() + ")";

            }
            catch (Exception ex)
            {
                CentaUtil.SendExceptionMessage(this.Page, ex);
            }
            finally
            {
                oK2Connection.Close();
            }
        }

        private void LoadWorkList1()
        {
            Connection oK2Connection = WF.Common.WorkFlowHelper.GetK2Connection();
            try
            {
                SessionManager sm = new SessionManager();
                DataSet ds = WorkFlowHelper.GetCurrentUserWorkListItem(oK2Connection, sm.CurrentUser.LoginName);
                ds.Tables[0].DefaultView.Sort = "ProcessName,StartDate Desc";
                this.dataListView.DataSource = ds.Tables[0].DefaultView;
                this.dataListView.DataBind();
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                oK2Connection.Close();
            }
        }

        public void ShowButton1()
        {
            dlCheckIn.Visible = true;
            DeptPathNolimited = System.Configuration.ConfigurationManager.AppSettings.Get("DeptPathNolimited");

            string[] arDeptPathNolimited = DeptPathNolimited.Split(';');
            foreach (string deptPathNolimited in arDeptPathNolimited)
            {
                if (((UserProfile)Session[SessionKeys.CurrentUser]).DeptPath != null)
                {
                    if (((UserProfile)Session[SessionKeys.CurrentUser]).DeptPath.IndexOf(deptPathNolimited) >= 0)
                    {
                        dlCheckIn.Visible = false;
                        return;
                    }
                }
                else
                {
                    return;
                }
            }
            
        }

        protected void aCheckIn_click(object sender, EventArgs e)
        {

            string SqlTxt = "";
            string UserAccount = "";
            string CardTime = "";
            string DispInfo = "";

            Com.DAO.DataManager pobj = new DataManager();

            try
            {
                CardTime = System.DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
                UserAccount = "centaline\\" + ((UserProfile) Session[SessionKeys.CurrentUser]).LoginName.ToString();
                SqlTxt = "exec  [dbo].[HandCardTime] '" + UserAccount + "','" + CardTime + "'";
                pobj.Open(System.Configuration.ConfigurationManager.AppSettings.Get("KQConnectionString"));
                pobj.Execute(SqlTxt);

                CardTime = "您的签卡时间为:" + CardTime;

            }
            catch (Exception ex)
            {
                CardTime = "签卡错误:" + ex.Message;
                CardTime = CardTime.Replace("\r", "");
                CardTime = CardTime.Replace("\n", "");
            }
            finally
            {
                try
                {
                    if (pobj != null && !pobj.IsClosed)
                    {
                        pobj.CloseConnection();
                    }
                }
                catch (Exception)
                {
                }
                CardTime = CardTime.Replace("'", "");
                DispInfo = "";
                DispInfo = DispInfo + "<script language='javascript'> \r\n";
                DispInfo = DispInfo + "		alert(\"" + CardTime + "\");\r\n ";
                DispInfo = DispInfo + "</script>\r\n";
                //Response.Write(DispInfo);
                this.ShowMessage(CardTime, false);
            }

        }
    }
}